package com.zhidou;

import java.io.Serializable;

import android.util.Log;

public class Ball implements Serializable {
    private static final long serialVersionUID = 8172012630142600740L;
    private int x;
    private int y;

    private int speedX;
    private int speedY;

    private String BALL_TAG = "Ball";

    public Ball() {
	x = 7;
	y = 7;
	speedX = 1;
	speedY = 1;
    }

    public Ball(TileView c, int x, int y, int speedX, int speedY) {
	super();
	this.x = x;
	this.y = y;
	this.speedX = speedX;
	this.speedY = speedY;
    }

    /**
     * move the ball and if lose the game then return false, else return true
     * 
     * @return 2 if ball turn, 1 if ball not turn ,0if ball out of board.
     */
    public int move() {
	boolean turn = false;
	if (y > TileView.mYTileCount + 1) {
	    // off the board and fail the game
	    Log.d(BALL_TAG, "ball lose!:" + this.toString() + ", TileView.mYTileCount:" + TileView.mYTileCount);
	    return 0;
	}

	if (x < 2 || x > TileView.mXTileCount - 3) {
	    turnX();
	    turn = true;
	}
	if (y < 1) {
	    turnY();
	    turn = true;
	}
	// Log.d(BALL_TAG, toString());
	x += speedX;
	y += speedY;
	return turn == true ? 2 : 1;
    }

    public void turnX() {
	speedX *= -1;
    }

    public void turnY() {
	speedY *= -1;
    }

    public String toString() {
	StringBuilder sb = new StringBuilder();
	sb.append("X:");
	sb.append(x);
	sb.append(",y:");
	sb.append(y);
	sb.append(", speedX:");
	sb.append(speedX);
	sb.append(", speedY:");
	sb.append(speedY);
	return sb.toString();
    }

    public int getX() {
	return x;
    }

    public void setX(int x) {
	this.x = x;
    }

    public int getY() {
	return y;
    }

    public void setY(int y) {
	this.y = y;
    }

    public int getSpeedX() {
	return speedX;
    }

    public void setSpeedX(int speedX) {
	this.speedX = speedX;
    }

    public void setSpeedY(int speedY) {
	this.speedY = speedY;
    }

}
